package services.daoImpl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;

import util.HibernateUtil;
import core.dao.Dao;

@Repository(value = "dao")
public class DaoImpl<T> implements Dao<T> {
	@Resource(name = "hibernateUtil")
	HibernateUtil hibernateUtil;

	@Override
	public List<T> select(Class<T> t, String hql) {
		// 每一次操作sql都有一个数据库会话session执行每一次crud都有其各自的事务顾session，transaction不能作为单例注入
		Session session = hibernateUtil.openSession();
		Transaction tr = session.beginTransaction();
		List<T> res = null;
		if (hql != null) {
			res = session.createQuery(hql).list();
		} else {
			res = session.createQuery("from " + t.getClass().getName()).list();
		}
		tr.commit();
		session.close();
		tr = null;
		return res;
	}

}
